package com.wsd.smartcarwasher.controller;
import com.wsd.smartcarwasher.util.IPUtils;
import com.wsd.smartcarwasher.util.IpToAddressUtil;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 * @author w
 * @Classname QueryWeatherController
 * @Description TODO
 * @Date 2020/2/18 18:46
 */
@Api(tags = "小程序——天气查询接口")
@RestController
public class QueryWeatherController {

    private static final Logger logger = LoggerFactory.getLogger(QueryWeatherController.class);


    @Autowired
    private RestTemplate restTemplate;


    @ApiOperation(value = "天气查询接口", notes = "天气查询接口")
    @RequestMapping(value = "/v1/QueryWeather", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String QueryWeather(){
        String apiURL = "https://restapi.amap.com/v3/weather/weatherInfo?city=610100&key=30e800ca04f9b7dd698ee0beb1ff8ee8";

        ResponseEntity<String> responseEntity =restTemplate.getForEntity(apiURL, String.class);

        if (200 == responseEntity.getStatusCodeValue()) {
            return responseEntity.getBody();
        } else {
            return "error with code : " + responseEntity.getStatusCodeValue();
        }
    }

    public static void main(String[] args) {
        String apiURL = "https://restapi.amap.com/v3/weather/weatherInfo?city=610100&key=30e800ca04f9b7dd698ee0beb1ff8ee8";
        RestTemplate restTemplate=new RestTemplate();
        ResponseEntity<String> responseEntity =restTemplate.getForEntity(apiURL, String.class);

        if (200 == responseEntity.getStatusCodeValue()) {
          //  System.out.println(responseEntity.getBody());
        }
    }


    @ApiOperation(value = "查询城市", notes = "返回最近的城市")
    @RequestMapping(value = "/v1/QueryCity", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public ModelMap QueryCity(HttpServletRequest request) {
        String ip= IPUtils.getIpAddr(request);
        logger.info(ip);
        logger.info(IpToAddressUtil.getCityInfo(ip));
        String addr=IpToAddressUtil.getCityInfo(ip);
       return ReturnUtil.Success(addr);
    }


}